<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogVisibleFile"
    width="30%"
    :close-on-click-modal="false"
    :show-close="false">
    <el-upload
      :action="url"
      :multiple="multiple"
      :on-preview="handlePreview"
      :on-remove="handleRemove"
      :before-remove="beforeRemove"
      :before-upload="beforeUpload"
      :limit="20"
      :on-exceed="handleExceed"
      :file-list="fileList">
      <el-button
        size="small"
        class="texBtn"
        type="text">点击上传</el-button>
      <div
        slot="tip"
        class="el-upload__tip">只能上传jpg/png/pdf文件，且不超过500kb</div>
    </el-upload>
    <span
      slot="footer"
      class="dialog-footer">
      <el-button @click="cancel">取 消</el-button>
      <el-button
        type="primary"
        class="submit"
        @click="submit">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  name: 'UploadFile',
  data () {
    return {
      fileList: [],
      name: ''
    }
  },
  props: {
    dialogVisibleFile: {
      default: false,
      type: Boolean
    },
    url: {
      default: '',
      type: String
    },
    multiple: {
      type: Boolean,
      default: true
    },
    title: {
      type: String,
      default: ''
    }
  },
  methods: {
    handleRemove (file, fileList) {
      //  console.log(file, fileList)
    },
    handlePreview (file) {
      // console.log(file)
    },
    handleExceed (files, fileList) {
      this.$message.warning(`当前限制选择 20 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
    },
    beforeRemove (file, fileList) {
      return this.$confirm(`确定移除 ${file.name}？`)
    },
    beforeUpload (file) {
      this.name = file.name
    },
    cancel () {
      this.$emit('closeDialog')
    },
    submit () {
      this.$emit('submitUpload', `${this.name}`)
    }
  }
}
</script>
<style lang="less" scoped>
@import '../../assets/var.less';

button {
  width: 74px;
  height: 34px;
  cursor: pointer;
  margin-left: 10px;
  outline: none;
  border-radius: 0;
}

.submit {
  background: @themeBlue;
  border: 0;
  color: #fff;
  margin-bottom: 15px;
  margin-left: 0;
}

.texBtn {
  color: @themeBlue;
}

.dangerBtn {
  color: @themeRed;
}
</style>
